! Configuration File for keepalived

global_defs {
{% if router_id is defined %}
    router_id {{ keepalived_router_id | d(inventory_hostname) }}
{% endif %}
{% if lvs_sync_daemon is defined %}
    lvs_sync_daemon {{ keepalived_interface | d(ansible_default_ipv4.interface) }}
{% endif %}
{% if lvs_timeouts is defined %}
    lvs_timeouts tcp {{ keepalived_lvs_timeouts | d("900") }}
{% endif %}

{% if keepalived_global_defs_script_user is defined %}
    script_user {{ keepalived_global_defs_script_user }}
{% endif %}
{% if keepalived_global_defs_enable_script_security is defined and keepalived_global_defs_enable_script_security | bool %}
    enable_script_security
{% endif %}
}

{% for key, value in keepalived_vrrp_scripts.items() %}
vrrp_script {{ key }} {
  script "{{ value.script }}"
{% if value.weight is defined %}
  weight {{ value.weight }}
{% endif %}
{% if value.interval is defined and value.interval | int %}
  interval {{ value.interval }}
{% endif %}
{% if value.timeout is defined and value.timeout | int %}
  timeout {{ value.timeout }}
{% endif %}
{% if value.rise is defined and value.rise | int %}
  rise {{ value.rise }}
{% endif %}
{% if value.fall is defined and value.fall | int %}
  fall {{ value.fall }}
{% endif %}
{% if value.user is defined %}
  user {{ value.user }}
{% endif %}
}
{% endfor %}


{% for inst in keepalived_vrrp_instance %}
vrrp_instance {{ inst.name | d('VI_1') }} {
    state {{ inst.state | d('MASTER') }}
    interface {{ inst.interface | d(ansible_default_ipv4.interface) }}
    virtual_router_id {{ inst.virtual_router_id | d('26') }}
    priority {% if inst.state == 'MASTER' %}{{ keepalived_priority | d('100') }}{% else %}{{ keepalived_priority | d('98') }}{% endif %}

    advert_int 1
    nopreempt

    unicast_src_ip {{ ansible_default_ipv4.address }}
    unicast_peer {    
    {% if ansible_default_ipv4.address == inst.unicast_peer_1 %}
    {{ groups['loadbalancer'][1] }}
    {% elif ansible_default_ipv4.address == inst.unicast_peer_2 %}
    {{ groups['loadbalancer'][0] }}
    {% endif %} 
    }

  

    virtual_ipaddress {
        {{ inst.vip }} dev {{ inst.interface | d(ansible_default_ipv4.interface) }} label {{ inst.interface | d(ansible_default_ipv4.interface) }}:1
    }
{% if inst.track_script is defined %}
    track_script {
    {% for track_script in inst.track_script %}
        {{ track_script }}
    {% endfor %}
    }
{% endif %}
    {{ inst.extra | d('') }}
}

{% endfor %}
{% if keepalived_virtual_server is defined %}
{% for vser in keepalived_virtual_server %}
virtual_server {{ vser.virtual_server }} {
    delay_loop {{ vser.delay_loop | d('6') }}
    lb_algo {{ vser.lb_algo | d('wrr') }}
    lb_kind {{ vser.lb_kind | d('NAT') }}
    persistence_timeout {{ vser.persistence_timeout | d('50') }}
    protocol {{ vser.protocol | d('TCP') }}

{% for rser in vser.real_server %}
    real_server {{ rser.server }} {
        weight {{ rser.weight | d('1') }}
{% if vser.protocol is undefined or vser.protocol == 'TCP'%}
        TCP_CHECK {
          connect_port {{ rser.tcp_check['connect_port'] | d('80') }}  
          connect_timeout {{ rser.tcp_check['connect_timeout'] | d('10') }}    
          retry {{ rser.tcp_check['retry'] | d('3') }}  
          delay_before_retry {{ rser.tcp_check['delay_before_retry'] | d('3') }}  
        }
{% endif %}
{% if rser.extra is defined %}
        {{ rser.extra | d('') }}
{% endif %}
    }
{% endfor %}
{% if vser.extra is defined %}
    {{ vser.extra | d('') }}
{% endif %}
}

{% endfor %}
{% endif %}
{{ keepalived_conf_extra }}